import java.awt.Point;
import java.awt.Rectangle;

/**
 * Jordan Lange
 */
public class Piece {
    private Tetris.Shape type;
    private Rectangle container; // Rectangle that shaped is contained within
    private int[][] constraint; // holds current constraint (-1 in constraint indicates
                                // a piece of the rectangle that isn't occupied)
    
    public Piece(Tetris.Shape _type, Point loc) {
        this.type = _type;
        this.container = new Rectangle(loc, type.getGridDimension());
        this.constraint = type.getConstraint();
    }
    
    public Piece(Tetris.Shape _type, Rectangle _container, int[][] _constraint) {
    	this.type = _type;
    	this.container = _container;
    	this.constraint = _constraint;
    }

    public Rectangle getContainer() {
        return this.container;
    }
    
    // GRID translation
    public void translate(int dx, int dy) {
        this.container.setLocation(new Point(container.x + dx, container.y + dy));
    }

    //  Performs a 90 degree rotation
    public void rotate() {
        // rotate container rectangle by interchanging size of rows/columns
        container.setSize(container.height, container.width);
        // update constraint
        int[][] newConstr = new int[container.width][container.height];
        for (int col = 0; col < constraint.length; col++) {
            for (int row = 0; row < constraint[col].length; row++) {
                if (constraint[col][row] == -1)
                    newConstr[row][constraint.length-col-1] = -1;
            }
        }
        this.constraint = newConstr;
    }
    public void rotateReverse() {
        // rotate container rectangle by interchanging size of rows/columns
        container.setSize(container.height, container.width);
        //						^					^
        //					New width			New height
        // update constraint
        int[][] newConstr = new int[container.width][container.height];
        for (int col = 0; col < constraint.length; col++) {
            for (int row = constraint[col].length - 1; row >= 0 ; row--) {
                if (constraint[col][row] == -1)
                    newConstr[container.width-row-1][col] = -1;
            }
        }
        this.constraint = newConstr;
    }

    // If a rotation is performed the constraint is updated, so the constraint given by the
    // enum will no longer be the correct one
    public int[][] getCurrentConstraint() {
        return this.constraint;
    }
    public void setConstraint(int[][] _constraint) {
    	this.constraint = _constraint;
    }
    public Tetris.Shape getType() {
        return this.type;
    }
    
    /*
     * Not needed now... maybe later?
    public Piece getCopy() {
        Tetris.Shape shape = Tetris.Shape.valueOf(this.type.name());
        Rectangle newRect = new Rectangle(this.container.x, this.container.y, this.container.width, this.container.height);
        int[][] newConstr = new int[this.constraint.length][this.constraint[0].length];
        for (int i = 0;i < this.constraint.length; i++) {
            for (int x = 0; x < this.constraint[0].length; x++) {
                newConstr[i][x] = new Integer(this.constraint[i][x]);
            }
        }
        return new Piece(shape, newRect, newConstr);
     }
     */
}
